package com.home.truc.code.process;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.home.Entity.Ban;
import com.home.Entity.TTGoiMon;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.TTGoiMonDAO;
import com.home.truc.code.components.MyItemBan;
import com.home.truc.code.components.MyItemThongTinGoiMon;
import com.home.truc.code.components.MyJPanelKhuVucBan;

public class MyControl {
	
	//phuong thuc don ban
	//gom hai thao tac la cap nhat tinh trang va tach ban
	public void DonBan(Ban ban, JTabbedPane panelKVB, JPanel panelGoiMon)
	{
		TTGoiMonDAO dao = new TTGoiMonDAO();
		// cap nhat tinh trang ban
		int c = dao.DemSoMonGoi(ban.getMaSo());
		if(c == 0)
		{
			//cap nhat tinh trang
			//cap nhat csdl
			ban.setTrangThai(false);
			new BanDAO().saveOrUpdate(ban);
			//cap nhat giao dien
			MyControl ctrl = new MyControl();
			ctrl.TimItemBan(ban.getMaSo(), panelKVB).CapNhatTinhTrang();
			
			//tach ban neu la ban ghep
			if(ban.getBanGhep() != 0)
				ctrl.TachBan(ban, panelKVB, panelGoiMon);
		}
	}
	//phuong thuc ghep hai ban lai voi nhau
	public void GhepBan(Ban b1, Ban b2, MyJPanelKhuVucBan kv, JTabbedPane tabpanelKVB, JPanel panelGoiMon)
	{
		long banGhep = b1.getMaSo();
		BanDAO dao = new BanDAO();
		long banGhepB1 = b1.getBanGhep();
		long banGhepB2 = b2.getBanGhep();
		
		if(banGhepB1 != 0)
		{
		
			List<Ban> L = dao.TimDSBanGhep(banGhepB1);
			for(Ban b : L)
			{
				b.setBanGhep(banGhep);
				dao.saveOrUpdate(b);
			}
		}
		if(banGhepB2 != 0)
		{
//			BanDAO dao = new BanDAO();
			List<Ban> L = dao.TimDSBanGhep(banGhepB2);
			for(Ban b : L)
			{
				b.setBanGhep(banGhep);
				dao.saveOrUpdate(b);
			}
		}
		
		b1.setBanGhep(banGhep);
		b2.setBanGhep(banGhep);
		
//		BanDAO dao = new BanDAO();
		dao.saveOrUpdate(b1);
		dao.saveOrUpdate(b2);
	
		LoadDanhSachBan(kv,tabpanelKVB, panelGoiMon);
	}
	//phuong thuc tach ban ghep
	//xoa ban ghep ra khoi khu vuc va them cac ban bi ghep vao
	public void TachBan(Ban ban, JTabbedPane panelKVB, JPanel panelGoiMon)
	{
		try
		{
			BanDAO daob = new BanDAO();
			//tach ban neu la ban ghep
			if(ban.getBanGhep() != 0)
			{
				List<Ban> L = daob.TimDSBanGhep(ban.getBanGhep());
				for(Ban b : L)
				{
					//cap nhat csdl
					b.setBanGhep(0);
					daob.saveOrUpdate(b);
				}
				LoadDanhSachBan(TimKhuVucBan(ban, panelKVB), panelKVB, panelGoiMon);
			}
		}catch(Exception e){JOptionPane.showInputDialog(e);}
	}
	
	//phuong thuc tim khu vuc co chua ban
	public MyJPanelKhuVucBan TimKhuVucBan(Ban b, JTabbedPane panelKVB)
	{
		int countkv = panelKVB.getComponentCount();
		for(int i = 0;i<countkv;i++)
		{
			MyJPanelKhuVucBan temp = (MyJPanelKhuVucBan)panelKVB.getComponent(i);
			if(temp.GetValue().getMaSo() == b.getKhuVucBan().getMaSo())
				return temp;
		}
		return null;
	}
	/*
	//phuong thuc xoa ban ra khoi khu vuc ban
	public void RemoveBan(Ban b, JTabbedPane panelKVB)
	{
		try {
		int countkv = panelKVB.getComponentCount();
		for(int i = 0;i<countkv;i++)
		{
			MyJPanelKhuVucBan kv = (MyJPanelKhuVucBan)panelKVB.getComponent(i);
			
			int countban = kv.getComponentCount();
			for(int k = 0; k<countban;k++)
			{
				MyItemBan item = (MyItemBan)kv.getComponent(k);
				if(item.getValue().getMaSo() == b.getMaSo())
					kv.remove(item);
			}
		}
		} catch (Exception e) {	}
	}*/
	// phuong thuc tim itemban trong panel khu vuc ban
	public MyItemBan TimItemBan(long masoban, JTabbedPane panelKVB)
	{
		int countkv = panelKVB.getComponentCount();
		for(int i = 0;i<countkv;i++)
		{
			MyJPanelKhuVucBan kv = (MyJPanelKhuVucBan)panelKVB.getComponent(i);
			
			int countban = kv.getComponentCount();
			for(int k = 0; k<countban;k++)
			{
				MyItemBan item = (MyItemBan)kv.getComponent(k);
				if(item.getValue().getMaSo() == masoban)
					return item;
			}
		}
		return null;
	}
	
	//phuogn thuc load danh sach ban
	public void LoadDanhSachBan(MyJPanelKhuVucBan kv,JTabbedPane tabbedPaneBan, JPanel panelGoiMon)
	{
		kv.RemoveAllItem();
		BanDAO bDao = new BanDAO();
		List<Ban> LBan = bDao.findByKVB(kv.GetValue().getMaSo());
		int count = LBan.size();
		for(int i = 0;i<count;i++)
		{
			Ban b = LBan.get(i);
			if(b.getBanGhep() == 0)
				ThemBan(kv, b,"");
			else
			{
				List<Ban> LBanGhep = new ArrayList<Ban>();
				for(int k = i; k<count;k++)
				{
					Ban temp = LBan.get(k);
					if(temp.getBanGhep() != 0 && temp.getBanGhep() == b.getBanGhep())
					{
						LBan.remove(k);
						count = LBan.size();
						k--;
						LBanGhep.add(temp);
					}
				}
				ThemBanGhep(LBanGhep, kv);
				i--;
			}
		}
		kv.SelectItemBan();
		LoadDanhSachGoiMon(tabbedPaneBan, panelGoiMon);
	}
	//them mot ban ban vao danh sach ban
	public void ThemBan(MyJPanelKhuVucBan kv, Ban b, String banGhep)
	{
		MyItemBan item = new MyItemBan(kv,b);
		item.SetTenBanGhep(banGhep);
		new BanDAO().CapNhatTrangThai(b);
		kv.add(item);
		if(kv.getSelectItem() == null)
			kv.SetSelectedItem(item);
	}
	public void ThemBanGhep(List<Ban> LBanGhep, MyJPanelKhuVucBan kv)
	{
		if(LBanGhep.size() != 0)
		{
			String s= "";
			for(Ban b : LBanGhep)
			{
				s += " "+b.getTen();
				ChuyenMon(LBanGhep.get(0), b);
				new BanDAO().CapNhatTrangThai(b);
			}
			new BanDAO().CapNhatTrangThai(LBanGhep.get(0));
			ThemBan(kv, LBanGhep.get(0), s);
		}
	}
	//phuong thuc chuyen danh sach goi mon tu ban b2 sang ban b1
	public void ChuyenMon(Ban b1, Ban b2)
	{
		try {
			TTGoiMonDAO dao = new TTGoiMonDAO();
			List<TTGoiMon> L = dao.TimDanhSachGoiMon(b2.getMaSo());
			if(L!=null)
			{
				for(TTGoiMon t : L)
				{
					t.setBan(b1);
					new TTGoiMonDAO().saveOrUpdate(t);
				}
			}
		} catch (Exception e) {
		}
	}
	
	//phuong thuc hien thi danh sach goi mon khi mo form ban hang
	public void LoadDanhSachGoiMon(JTabbedPane tabbedPaneBan, JPanel panelGoiMon)
	{
		try {
			panelGoiMon.removeAll();
			
			TTGoiMonDAO dao = new TTGoiMonDAO();
			List<TTGoiMon> L = dao.findAll(TTGoiMon.class);
			for(TTGoiMon item : L)
			{
				panelGoiMon.add(new MyItemThongTinGoiMon(item, panelGoiMon, tabbedPaneBan));
				panelGoiMon.updateUI();
			}
		} catch (Exception e) {
		}
	}

}
